package com.spring.demo.weixinpay.service.impl;

import com.spring.demo.weixinpay.entity.Redpacket;
import com.spring.demo.weixinpay.service.RedPacketService;
import com.spring.demo.weixinpay.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Map;
import java.util.SortedMap;
import java.util.UUID;

public class RedPacketServiceImpl implements RedPacketService{

    @Override
    public Map<String, String> sendXml(String openid) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
        Redpacket redpacket=new Redpacket();
        redpacket.setAct_name("活动红包");
        redpacket.setMch_id("商户号");
        redpacket.setNonce_str(UUID.randomUUID().toString().replace("-",""));
        redpacket.setClient_ip("客户端ip");
        redpacket.setRe_openid(openid);
        redpacket.setRemark("备注");
        redpacket.setSend_name("发送名称");
        redpacket.setMch_billno("订单号");
        redpacket.setRisk_info("活动名称");
        redpacket.setWishing("祝福语句");
        redpacket.setTotal_amount(1.00);
        redpacket.setTotal_num(1);
        redpacket.setWxappid("公众号appid");
        SortedMap<Object, Object> redpacketMap = CommonUtil.convertBean(redpacket);
        String sign = CommonUtil.createSgin(redpacketMap);
        String result="";
        String sb=CommonUtil.httpsRequest("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack","post",result,true).toString();
        return  CommonUtil.parseXml(sb.toString());
    }
}
